<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item  prop="userName">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item  prop="nickName">
          <el-input
            v-model="queryParams.nickName"
            placeholder="请输入会员昵称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item  prop="mobile">
          <el-input
            v-model="queryParams.mobile"
            placeholder="请输入手机号码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item  prop="sex">
          <el-select v-model="queryParams.sex" placeholder="请选择会员性别" clearable>
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item  prop="grade">
          <el-select v-model="queryParams.grade" placeholder="请选择会员等级" clearable>
            <el-option
              v-for="dict in gradeList"
              :key="dict.gradeLevel"
              :label="dict.gradeName"
              :value="dict.gradeLevel"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="memberList" >
        <el-table-column type="selection" width="55" align="center">
          <template slot-scope="scope">
            <i class="el-icon-circle-check" v-if="scope.row.selected" style="font-size: 20px;color: red"></i>
          </template>
        </el-table-column>
        <el-table-column label="会员头像" align="center" prop="avatar">
          <template slot-scope="scope">
            <el-image style="width: 60px;border-radius: 12px" fit="contain" :src="scope.row.avatar"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="用户名" align="center" prop="userName"/>
        <el-table-column label="会员昵称" align="center" prop="nickName"/>
        <el-table-column label="手机号码" align="center" prop="mobile"/>
        <el-table-column label="会员等级" align="center" prop="grade">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
          </template>
        </el-table-column>
        <el-table-column label="会员性别" align="center" prop="sex">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
          </template>
        </el-table-column>
        <el-table-column label="会员生日" align="center" prop="birthday" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否有效" align="center" prop="isEnable">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_is_enable" :value="scope.row.isEnable"/>
          </template>
        </el-table-column>
        <el-table-column label="客户端" align="center" prop="clientEnum"/>
        <el-table-column label="最后一次登录时间" align="center" prop="lastLoginDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.lastLoginDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              v-if="!scope.row.selected"
              @click="handChecke(scope.row)"
            >选中</el-button>

            <el-button
              size="mini"
              type="text"
              icon="el-icon-close"
              v-if="scope.row.selected"
              @click="handChecke(scope.row)"
            >取消</el-button>
          </template>

        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <div slot="footer" style="text-align: right;margin-top: 15px">
      <el-button type="primary" @click="handleChoose">确 定</el-button>
    </div>
  </div>
</template>

<script>
import {listMember} from "@/api/uac/member";
import {listGradeAll} from "@/api/uac/grade";

export default {
  name: "Member",
  dicts: ['sys_user_sex', 'sys_is_enable'],
  props: {
    selectedWay: {
      type: Array,
      default: function () {
        return new Array();
      },
    },
  },
  data() {
    return {
      type: "multiple", //单选或者多选 single  multiple
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员表格数据
      memberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 会员详情弹出层
      memberInfoOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        nickName: null,
        password: null,
        mobile: null,
        email: null,
        avatar: null,
        sex: null,
        grade: null,
        birthday: null,
        isEnable: 1,
        clientEnum: null,
        lastLoginDate: null,
        wxMpOpenid: null,
        wxXcxOpenid: null,
      },
      gradeList: [],
      memberId: null
    };
  },
  created() {
    this._listGradeAll();
    this.getList();
  },
  methods: {
    /** 查询会员列表 */
    getList() {
      this.loading = true;
      let _this = this;
      listMember(this.queryParams).then(response => {

        response.rows.forEach((item) => {
          item.selected = false;
          _this.selectedWay.forEach((e) => {
            if (e.id && e.id === item.id) {
              item.selected = true;
            }
          });
        });

        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /* 加载全部会员等级 */
    _listGradeAll() {
      listGradeAll().then(response => {
        this.gradeList = response.data;
      });
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 选中按钮操作 */
    handleChoose() {
      this.$emit("selectedMemberData", this.selectedWay);
    },

    /**
     * 选中事件
     */
    handChecke(row) {
      // 如果单选的话
      if (this.type != "multiple") {
        this.memberList.forEach((item) => {
          item.selected = false;
        });
        this.selectedWay = [];
        row.selected = true;
        this.selectedWay.push(row);

        return false;
      }

      if (row.selected == false) {
        row.selected = true;
        row.num = 1
        this.selectedWay.push(row);
      } else {
        row.selected = false;
        for (let i = 0; i < this.selectedWay.length; i++) {
          if (this.selectedWay[i].id === row.id) {
            this.selectedWay.splice(i, 1);
            break;
          }
        }
      }
    },
  }
};
</script>
